package com.zf.yichat.api.controller.carbonForest;

import com.github.pagehelper.PageHelper;
import com.zf.yichat.api.controller.BaseController;
import com.zf.yichat.api.dto.request.UserAnswerRequest;
import com.zf.yichat.api.dto.request.UserApplyCertificateRequest;
import com.zf.yichat.dto.StQuestionsDto;
import com.zf.yichat.model.StLowCarbonApplyCertificate;
import com.zf.yichat.model.SysDict;
import com.zf.yichat.service.SysDictService;
import com.zf.yichat.service.UserAnswerService;
import com.zf.yichat.service.UserService;
import com.zf.yichat.utils.response.FsResponse;
import com.zf.yichat.utils.response.FsResponseGen;
import com.zf.yichat.vo.DictKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 用户答题功能
 */
@RestController
@RequestMapping("answer")
@Slf4j
public class UserAnswerController extends BaseController {

    @Autowired
    private UserAnswerService userAnswerService;

    @Autowired
    private SysDictService sysDictService;

    @Autowired
    private UserService userService;

    /**
     * 获取题目列表
     * @return
     */
    @PostMapping("queryQuestionList")
    public FsResponse queryUserPlantTreeConfig() {
        Long userId = getUserId();
        if (null == userId) {
            return FsResponseGen.failMsg("参数错误【userId】");
        }

        StQuestionsDto questions = userAnswerService.queryQuestionList(userId);
        return FsResponseGen.successData(questions);
    }

    /**
     * 用户答题
     * @return
     */
    @PostMapping("userAnswerQuestion")
    public FsResponse userAnswerQuestion(@RequestBody UserAnswerRequest request) {

        Long userId = getUserId();
        if (null == userId) {
            return FsResponseGen.failMsg("参数错误【userId】");
        }

        Integer answerId = request.getAnswerId();
        if (null == answerId) {
            return FsResponseGen.failMsg("参数错误【answerId】");
        }

        return userAnswerService.userAnswerQuestion(userId, answerId);
    }


    /**
     * 获取答题规则
     * @return
     */
    @PostMapping("queryAnswerRole")
    public FsResponse queryAnswerRole() {
        SysDict dict = sysDictService.selectOne(DictKey.user_answer_role);
        if (null == dict) {
            return FsResponseGen.successData("暂无配置！");
        }
        return FsResponseGen.successData(dict.getMemo());
    }

    /**
     * 获取答题提示语
     * @return
     */
    @PostMapping("queryAnswerPrompt")
    public FsResponse queryAnswerPrompt() {
        SysDict dict = sysDictService.selectOne(DictKey.user_answer_prompt);
        if (null == dict) {
            return FsResponseGen.successData("暂无配置！");
        }
        return FsResponseGen.successData(dict.getMemo());
    }

    /**
     * 获取碳能量获取规则
     * @return
     */
    @PostMapping("queryCarbonAcquisitionRules")
    public FsResponse queryCarbonAcquisitionRules() {
        SysDict dict = sysDictService.selectOne(DictKey.carbon_energy_acquisition_rules);
        if (null == dict) {
            return FsResponseGen.successData("暂无配置！");
        }
        return FsResponseGen.successData(dict.getMemo());
    }

    /**
     * 查询用户答题正确数
     * @return
     */
    @PostMapping("queryUserAnswerCount")
    public FsResponse queryUserAnswerCount() {
        Long userId = getUserId();
        if (null == userId) {
            return FsResponseGen.failMsg("参数错误【userId】");
        }

        return FsResponseGen.successData(userAnswerService.queryUserAnswerCount(userId));
    }

    /**
     * 用户申请\查看实践证书
     * @return
     */
    @PostMapping("userApplyCertificate")
    public FsResponse userApplyCertificate(@RequestBody UserApplyCertificateRequest request) {
        Long userId = getUserId();
        if (null == userId) {
            return FsResponseGen.failMsg("参数错误【userId】");
        }

        if (!userService.realValidStatus(userId)) {
            return FsResponseGen.failMsg("请先实名认证");
        }

        StLowCarbonApplyCertificate certificate = new StLowCarbonApplyCertificate();
        BeanUtils.copyProperties(request, certificate);
        certificate.setUserId(userId);
        return userAnswerService.userApplyCertificate(certificate);
    }

    /**
     * 查询低碳知识列表
     * @return
     */
    @PostMapping("queryStLowCarbonKnowledgeList")
    public FsResponse queryStLowCarbonKnowledgeList(@RequestBody UserAnswerRequest request) {

        Integer pageNo = request.getPageNo();
        Integer pageSize = request.getPageSize();
        if (null == pageNo) {
            pageNo = 1;
        }
        if (null == pageSize) {
            pageSize = 10;
        }

        PageHelper.startPage(pageNo, pageSize);
        return userAnswerService.queryStLowCarbonKnowledgeList();
    }


}
